package linuxbox.admin.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import linuxbox.BoxStatic;
import linuxbox.admin.logic.*;

/**
 * This class displays a user list.
 */
public class UserList extends TagSupport {
    public static final String SORT_BY_NAME = "NAME";
    public static final String SORT_BY_PHONE = "PHONE";
    public static final String SORT_BY_DEPT = "DEPT";
    public static final String SORT_BY_EMAIL = "EMAIL";

    protected String listType = null;
    protected String sortType = SORT_BY_NAME;
    protected String userType = BoxStatic.USER_TYPE_ALL;
    protected String privilegeType = BoxStatic.PRIVILEGE_TYPE_USER;

    public int doStartTag() throws JspException {
        try {
            UserManager um = UserManager.getInstance();
            int[] ids = null;

            if (BoxStatic.USER_TYPE_ALL.equalsIgnoreCase(userType)) {
                if (SORT_BY_NAME.equalsIgnoreCase(sortType)) {
                    ids = um.getAllUsers(" order by lastname, firstname");
                } else if (SORT_BY_PHONE.equalsIgnoreCase(sortType)) {
                    ids = um.getAllUsers(" order by phone");
                } else if (SORT_BY_DEPT.equalsIgnoreCase(sortType)) {
                    ids = um.getAllUsers(" order by dept_id");
                } else if (SORT_BY_EMAIL.equalsIgnoreCase(sortType)) {
                    ids = um.getAllUsers(" order by email");
                } else {
                    ids = um.getAllUsers();
                }
            }

            JspWriter out = pageContext.getOut();
            User user = null;

            if (BoxStatic.DISPLAY_TYPE_TABLE.equalsIgnoreCase(listType)) {
                for (int i = 0; i < ids.length; i++) {
                    if ((i % 2) != 0)
                        out.println("<TR bgcolor=\"#E0E0E0\">");
                    else
                        out.println("<TR>");

                    user = um.getUser(ids[i]);
                    String action = "UserShowAction";
                    if (BoxStatic.PRIVILEGE_TYPE_ADMIN.equalsIgnoreCase(privilegeType)) {
                        action = "UserManEditAction";
                    }
                    out.println("<TD><A href=\""+BoxStatic.servletDir+action
                        + "?USER_ID="
                        + user.getID() + "\">" + user.getFirstName() + ", "
                        + user.getLastName() + "</A></TD>");
                    out.newLine();
                    out.println("<TD>" + user.getPhone() + "</TD>");
                    out.newLine();
                    out.println("<TD>" + user.getEmail() + "</TD>");
                    out.newLine();
                    out.println("<TD>" + user.getDeptName() + "</TD>");
                    out.newLine();
                    out.println("</TR>");out.newLine();
                }
            } else if (BoxStatic.DISPLAY_TYPE_LIST.equalsIgnoreCase(listType)) {
                for (int i = 0; i < ids.length; i++) {
                    if ((i % 2) != 0)
                        out.println("<TR bgcolor=\"#E0E0E0\">");
                    else
                        out.println("<TR>");

                    user = um.getUser(ids[i]);
                    out.println("<TD><A href=\""+BoxStatic.servletDir+"UserManEditAction?USER_ID="
                        + user.getID() + "\">" + user.getLoginID() + "</A></TD>");
                    out.newLine();
                    out.println("<TD>" + user.getFirstName() + "</TD>");
                    out.newLine();
                    out.println("<TD>" + user.getLastName() + "</TD>");
                    out.newLine();
                    out.println("<TD><a href=\""+BoxStatic.servletDir+"UserManDelAction?USER_ID="
                        + user.getID() + "\">Delete</a></TD>");
                    out.newLine();
                    out.println("</TR>");out.newLine();
                }
            }
            out.flush();
        } catch(Exception e) {
            BoxStatic.logger.error("Failed to get all users");
            throw new JspException(e.getMessage());
        }

        return SKIP_BODY;
    }

    public int doEndTag() throws JspException {
        return super.doEndTag();
    }

    public void setListType(String aStr) {
        listType = aStr;
    }

    public void setSortType(String aStr) {
        sortType = aStr;
    }

    public void setUserType(String aStr) {
        userType = aStr;
    }

    public void setPrivilegeType(String aStr) {
        privilegeType = aStr;
    }
}
